package de.medizin.uni.halle.irm;

import android.content.Context;
import android.content.res.Resources;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragment;

public class OverviewFragment extends SherlockFragment{
	private ArrayAdapter<String> listAdapter ;  
	private SQLiteDatabase database;
	private Cursor c;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
    	View v = inflater.inflate(R.layout.overview, container, false);
    	update_mail_list(v);
    	update_sms_list(v);
    	update_phone_list(v);
    	update_letter_list(v);
        return v;
    }
    
    private void update_mail_list(View v){
    	database = getSherlockActivity().openOrCreateDatabase("born", Context.MODE_PRIVATE, null);
    	Resources res = getResources();
    	ListView lv  = (ListView) v.findViewById(R.id.overview_list_email);
    	lv.setOverScrollMode(View.OVER_SCROLL_NEVER);
    	c = database.rawQuery("Select p._id, p.sid, p.firstname, p.lastname, p.rank, pl.date from patients p, plans pl where pl.contacttype=1 and p._id=pl.patient_id and julianday('now') >= julianday(pl.date) order by date;" , null);
    	int sm = c.getCount();
    	c = database.rawQuery("SELECT * FROM history WHERE historytype = 'email received' AND evaluation IS NULL " , null);
    	int rm = c.getCount();
    	c = database.rawQuery("SELECT * FROM history WHERE historytype = 'email sent';" , null);
    	int rsm = c.getCount();
    	String[] mails = new String[] {String.format(res.getString(R.string.overview_send_emails), sm),
    			String.format(res.getString(R.string.overview_sent_emails), rsm),
    			String.format(res.getString(R.string.overview_received_emails), rm)}; 
        listAdapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item_overview, mails);  
        lv.setAdapter( listAdapter );  
        onclick(lv, 3);
        database.close();
    }
     	
	private void update_phone_list(View v){
    	database = getSherlockActivity().openOrCreateDatabase("born", Context.MODE_PRIVATE, null);
    	ListView lv  = (ListView) v.findViewById(R.id.overview_list_phone);
    	lv.setOverScrollMode(View.OVER_SCROLL_NEVER);
    	Resources res = getResources();
    	c = database.rawQuery("Select p._id, p.sid, p.firstname, p.lastname, p.rank, pl.date from patients p, plans pl where pl.contacttype=3 and p._id=pl.patient_id and julianday('now') >= julianday(pl.date) order by date;" , null);
    	int sm = c.getCount();
    	c = database.rawQuery("SELECT * FROM history WHERE historytype = 'succesfull call' AND evaluation IS NULL " , null);
    	int rm = c.getCount();
    	String[] mails = new String[] {String.format(res.getString(R.string.calls_to_make), sm),
    			String.format(res.getString(R.string.successfull_calls), rm)}; 
    	
        listAdapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item_overview, mails);  
        lv.setAdapter( listAdapter );  
        onclick(lv, 5);
        database.close();
    }
    
    private void update_sms_list(View v){
    	database = getSherlockActivity().openOrCreateDatabase("born", Context.MODE_PRIVATE, null);
    	Resources res = getResources();
    	ListView lv  = (ListView) v.findViewById(R.id.overview_list_sms);
    	lv.setOverScrollMode(View.OVER_SCROLL_NEVER);
    	c = database.rawQuery("Select p._id, p.sid, p.firstname, p.lastname, p.rank, pl.date from patients p, plans pl where pl.contacttype=2 and p._id=pl.patient_id and julianday('now') >= julianday(pl.date) order by date;" , null);
    	int sm = c.getCount();
    	c = database.rawQuery("SELECT * FROM history WHERE historytype = 'sms received' AND evaluation IS NULL " , null);
    	int rm = c.getCount();
    	c = database.rawQuery("SELECT * FROM history WHERE historytype = 'sms sent';" , null);
    	int rsm = c.getCount();
    	String[] sms = new String[] {String.format(res.getString(R.string.overview_send_emails), sm),
    			String.format(res.getString(R.string.overview_sent_sms), rsm),
    			String.format(res.getString(R.string.overview_received_sms), rm)}; 
        listAdapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item_overview, sms);  
        lv.setAdapter( listAdapter );        
        onclick(lv, 4);
        database.close();
    }
    
    private void update_letter_list(View v){
    	ListView lv  = (ListView) v.findViewById(R.id.overview_list_letter);
    	lv.setOverScrollMode(View.OVER_SCROLL_NEVER);
    	String[] mails = new String[] { "Zu versendende Briefe (5)", "Unbeantwortete Briefe (2)"}; 

        listAdapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item_overview, mails);  
        lv.setAdapter( listAdapter );  
    }
    
    private void onclick(ListView lv, final int p) {
	    lv.setOnItemClickListener(new OnItemClickListener()
	    {
	    	public void onItemClick(AdapterView<?> arg0, View v, int position, long id)
	    	{	
	    		ActionBar actionbar = getSherlockActivity().getSupportActionBar();
	    		BornActivity.setPosition(position);
	    		actionbar.selectTab(actionbar.getTabAt(p));
	        }
	     });
	}    
}
